import asyncio

import motor.motor_asyncio
import time


async def main():
    client = motor.motor_asyncio.AsyncIOMotorClient(
        "mongodb://rwuser:Gr#54WE24KL!dfjh5!!lkfg874@localhost:27017/bigdata?authSource=admin&directConnection=true"
    )
    db = client["bigdata"]

    gt_task_col = db["annotated_ground_truth_task"]
    gt_task_item_col = db["annotated_ground_truth_task_item"]

    tasks = (
        await gt_task_col.find({}, {"task_detail": 0, "clip_ids": 0})
        .limit(100)
        .to_list()
    )

    t1 = time.time()
    for task in tasks:
        await gt_task_item_col.find({"run_id": task["task_name"]}).to_list()
    t2 = time.time()
    print("seq cost", t2 - t1, "seconds")

    t1 = time.time()
    cor_list = []
    for task in tasks:
        cor_list.append(gt_task_item_col.find({"run_id": task["task_name"]}).to_list())
    await asyncio.gather(*cor_list)
    t2 = time.time()
    print("gather cost", t2 - t1, "seconds")


if __name__ == "__main__":
    asyncio.run(main())
